﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using Cat;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmSupplierEntry : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbcon = new DatabaseConnection();
        public frmSupplierEntry()
        {
          
            InitializeComponent();
            this.Load += new EventHandler(frmSupplierEntry_Load);
            //Tab1 button
            btnNew.Click += new EventHandler(btnNew_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            
            //Tab2 button
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            btnClear.Click += new EventHandler(btnClear_Click);
            //Tab2 textbox
            txtsearchuser.TextChanged += new EventHandler(txtsearchuser_TextChanged);
            //Tab2 grid
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
        }
        //Tab2 grid function
        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            txtSuppIDU.Text = dr.Cells[0].Value.ToString();
            txtSuppNameU.Text = dr.Cells[1].Value.ToString();
            txtContactU.Text = dr.Cells[3].Value.ToString();
            txtAddressU.Text = dr.Cells[2].Value.ToString();
            txtLandlineU.Text = dr.Cells[4].Value.ToString();
            txtEmailU.Text = dr.Cells[5].Value.ToString();
            
        }
        //tab2 textbox function
        void txtsearchuser_TextChanged(object sender, EventArgs e)
        {
            if (txtsearchuser.Text != "Search Supplier")
            {
                dbcon.dbconnection();
                dbcon.loadData("SELECT distinct SupplierID,SupplierName,Address,Contactno,LandLine,Email,Encodedby FROM tblsupplier WHERE SupplierName LIKE '%" + txtsearchuser.Text + "%'");
                dataGridView1.DataSource = dbcon.tbl;
                dbcon.DBClose();
            }
            else
            {
                getdata();
            }   
        }
        //Tab2 Button Function
        void btnClear_Click(object sender, EventArgs e)
        {
            resetup();
        }
        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtSuppIDU.Text == "")
            {
                MessageBox.Show("Please Select Supplier ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            if (txtSuppNameU.Text == "")
            {
                MessageBox.Show("Please Enter Supplier Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            if (txtAddressU.Text == "")
            {
                MessageBox.Show("Please Enter Supplier address ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            if (txtContactU.Text == "")
            {
                MessageBox.Show("Please Enter Contact number of suppplier", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            if (txtLandlineU.Text == "")
            {
                MessageBox.Show("Please Enter Landline number of supplier", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            if (txtEmailU.Text == "")
            {
                MessageBox.Show("Please Enter Email of supplier", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearchuser.Focus();
                return;
            }
            dbcon.dbconnection();
            dbcon.UpdateData("Update tblsupplier set SupplierName = '" + txtSuppNameU.Text + "', Address = '" + txtAddressU.Text + "', Contactno  = '" + txtContactU.Text + "', LandLine = '" + txtLandlineU.Text + "', Email = '" + txtEmailU.Text + "' Where SupplierID='" + txtSuppIDU.Text + "';");
            MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            resetup();
            dbcon.DBClose();
            getdata();
        }
        //Tab1 Button Function
        
        void btnSave_Click(object sender, EventArgs e)
        {
            
            if (txtSupplierName.Text == "")
            {
                MessageBox.Show("Please Enter Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSupplierName.Focus();
                return;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Please Enter Address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAddress.Focus();
                return;
            }
            if (txtContactNo.Text == "")
            {
                MessageBox.Show("Please Enter Contact Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContactNo.Focus();
                return;
            }
            if (txtLandline.Text == "")
            {
                MessageBox.Show("Please Enter Land Line Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtLandline.Focus();
                return;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Please Enter Email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }
            con = new OdbcConnection(cs.DBConn);
            con.Open();
            string ct = "Select SupplierName from tblsupplier where SupplierName='" + txtSupplierName.Text + "'";
            cmd = new OdbcCommand(ct);
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                MessageBox.Show("Supplier Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSupplierName.Text = "";
                txtSupplierName.Focus();

                if ((rdr != null))
                {
                    rdr.Close();
                }
                return;
            }
            con.Close();

            dbcon.dbconnection();
            dbcon.AddData("Insert into tblsupplier(SupplierID,SupplierName,Address,Contactno,LandLine,Email,Encodedby)" + "VALUES ('" + txtSupplierID.Text + "','" + txtSupplierName.Text + "','" + txtAddress.Text + "','" + txtContactNo.Text + "','" + txtLandline.Text + "','"+txtEmail.Text+"',)");
            MessageBox.Show("Successfully added", "Product", MessageBoxButtons.OK, MessageBoxIcon.Information);
            reset();
            autoid();
            dbcon.DBClose();
        }
        void btnNew_Click(object sender, EventArgs e)
        {
            reset();
            autoid();
            btnSave.Enabled = true;
            
        }
        //Auto ID and Reset
       public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars = "123456789".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
       void autoid()
        { 
            txtSupplierID.Text = "SUP-" + GetUniqueKey(5);
        
        }
       void reset()
       {
           txtSupplierName.Text = "";
           txtContactNo.Text = "";
           txtAddress.Text = "";
           txtLandline.Text = "";
           txtEmail.Text = "";
          
       
       }
       void resetup()
       {
           txtSuppIDU.Text = null;
           txtSuppNameU.Text = "";
           txtContactU.Text = "";
           txtAddressU.Text = "";
           txtLandlineU.Text = "";
           txtEmailU.Text = "";
      
       }
       
       void getdata()
       {
           dbcon.dbconnection();
           dbcon.loadData("Select distinct SupplierID,SupplierName,Address,Contactno,LandLine,Email,Encodedby  from tblsupplier");
           dataGridView1.DataSource = dbcon.tbl;
           dbcon.DBClose();
       }
       void frmSupplierEntry_Load(object sender, EventArgs e)
       {
           autoid();
           dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
           dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
           getdata();
       }
    }
}
